require('./filter');
var Router = require('koa-router');
var service = require('./service');

var router = module.exports = new Router({
  prefix: '/delivery'
});

// 派送单列表
router.get('/',loginRequired,cacheDisabled, function * (next) {
  var projects = this.session.projects;
  var projectIds = [];
  if (projects && projects.length > 0) {
    for(var i = 0; i < projects.length; i++){
      projectIds.push(projects[i].id);
    }
  }
 
  var status = this.query.status;
  var params = {
    mid:this.session.member.memberId,
    status:status || 0,
    projectIds:projectIds,
    pageNo:1,
    pageSize:20
  }
 
  var data = yield {
    expresses:service.page4HK(this, params),
    curProject:'',
    projects:this.session.projects
  };
 
  if (status == 2) {
    this.body = this.render('templates/cancelsign.html',data);
  }else if (status == 1) {
    this.body = this.render('templates/signed.html',data);
  }else{
    this.body = this.render('templates/sign.html',data);
  }
  
});

// 派送单列表
// router.get('/asdfasd',loginRequired,cacheDisabled, function * (next) {
//   var projects = this.session.projects;
//   var projectIds = [];
//   if (projects && projects.length > 0) {
//     for(var i = 0; i < projects.length; i++){
//       projectIds.push(projects[i].id);
//     }
//   }

//   console.log('===============================')
//   console.log(this.query)
//   var params1 = {
//     mid:this.session.member.memberId,
//     status:0,
//     projectIds:projectIds,
//     pageNo:1,
//     pageSize:20
//   }
//   var params2 = {
//     mid:this.session.member.memberId,
//     status:1,
//     projectIds:projectIds,
//     pageNo:1,
//     pageSize:20
//   }
//   var params3 = {
//     mid:this.session.member.memberId,
//     status:2,
//     projectIds:projectIds,
//     pageNo:1,
//     pageSize:20
//   }

  
//   var data = yield {
//     unsign:service.page4HK(this, params1),
//     signed:service.page4HK(this,params2),
//     signcancel:service.page4HK(this,params3),
//     curProject:'',
//     projects:this.session.projects
//   };
  
//   this.body = this.render('templates/home.html',data);
// });



// 待抢单
router.get('/ajax/unsign',loginRequired, function * (next) {
  var projects = this.session.projects;
  var projectIds = [];
  if (projects && projects.length > 0) {
    for(var i = 0; i < projects.length; i++){
      projectIds.push(projects[i].id);
    }
  }
  var params = {
    mid:this.session.member.memberId,
    status:0,
    projectIds:projectIds,
    pageNo:this.query.page,
    pageSize:this.query.size
  }
  var data = yield service.page4HK(this, params);
  this.body = this.render(200,{
    list:data.list
  });
});

// 已抢单
router.get('/ajax/signed',loginRequired, function * (next) {
  var projects = this.session.projects;
  var projectIds = [];
  if (projects && projects.length > 0) {
    for(var i = 0; i < projects.length; i++){
      projectIds.push(projects[i].id);
    }
  }
  var params = {
    mid:this.session.member.memberId,
    status:1,
    projectIds:projectIds,
    pageNo:this.query.page,
    pageSize:this.query.size
  }
  var data = yield service.page4HK(this, params);
  this.body = this.render(200,{
    list:data.list
  });
});

// 已取消
router.get('/ajax/signcancel',loginRequired, function * (next) {
  var projects = this.session.projects;
  var projectIds = [];
  if (projects && projects.length > 0) {
    for(var i = 0; i < projects.length; i++){
      projectIds.push(projects[i].id);
    }
  }
  var params = {
    mid:this.session.member.memberId,
    status:2,
    projectIds:projectIds,
    pageNo:this.query.page,
    pageSize:this.query.size
  }
  
  var data = yield service.page4HK(this, params);
  this.body = this.render(200,{
    list:data.list
  });
});

// 抢单
router.post('/ajax/grab', function * (next) {
  var self = this;
  var form = this.request.body;
  var partners = this.session.partners;
  var puserId = '';
  if ( partners && partners.length > 0) {
    for(var i = 0; i < partners.length; i++){
      if (partners[i].partnerType == 'bs') {
        puserId = partners[i].partnerId;
      }
    }
  }
  var params = {
  	id:form.id,
  	// url:form.url,
  	mid:this.session.member.memberId,
  	projectId:form.projectId,
    puserId:puserId
  }
  var data = yield service.grabExpress(this, params).then(function(data){
    self.body = self.render(200,data);
  },function(data){
    self.body = self.render(500,data);
  });
});

//扫码成功
router.get('/success', function * (next){
  this.body = this.render('templates/process.html', this.query)
})

//扫码成功
router.get('/successpage', function * (next){
  this.body = this.render('templates/success.html', this.query)
})

//扫码成功
router.get('/code/isvalid', function * (next){
  var self = this;
  var params = {
    id:this.query.id,
    mid:this.session.member.memberId
  }
  
  var data = null;
  if (this.query.optType == 1) {
    data = yield service.receive(this,params).then(function(res){
      self.body = self.render(200, res)
    }, function(rej){
      self.body = self.render(500, rej)
    })
  }else{
    data = yield service.giveBack(this,params).then(function(res){
      self.body = self.render(200, res)
    },function(rej){
      self.body = self.render(500, rej)
    })
  }

})


//扫码失败
router.get('/error', function * (next){
  this.body = this.render('templates/error.html', this.query)
})


// 取消配送
router.post('/ajax/cancelHK', function * (next) {
  var self = this;
  var form = this.request.body;
  var params = {
  	id:form.id
  }
  var data = yield service.cancel4HK(this, params).then(function(data){
    self.body = self.render(200,data);
  },function(data){
    self.body = self.render(500,data);
  });
  
});

// 快递详情
router.get('/details/:id', function * (next) {
  var data = yield service.deliveryDetails(this, {id:this.params.id});
  this.body = this.render('templates/details.html',data);
});

// 配送状态
router.get('/status/:id', function * (next) {
  var data = yield service.getStatusChangeChain4App(this, {id:this.params.id});
  this.body = this.render('templates/status.html',data);
});

// 配送单详情
router.get('/expressd/:id', function * (next) {
  var data = yield service.expressDetail(this, {code:this.params.id});
  this.body = this.render('templates/expressd.html',data);
});


// 支付二维码
router.get('/code/:id', function * (next) {
  var params = {
  	id:this.params.id
  }
  var data = yield service.createQRCode(this, params);
  data.entity = "data:image/png;base64," + data.entity;
  this.body = this.render('templates/code.html',{
  	id:this.params.id
  },data);
});

// 群组公告
router.get('/:id/:project',loginRequired,cacheDisabled, function * (next) {
  var projectIds = [];
  if (this.params.id) {
    projectIds.push(this.params.id)
  }

  var params1 = {
    mid:this.session.member.memberId,
    status:0,
    projectIds:projectIds,
    pageNo:1,
    pageSize:20
  }
  var params2 = {
    mid:this.session.member.memberId,
    status:1,
    projectIds:projectIds,
    pageNo:1,
    pageSize:20
  }
  var params3 = {
    mid:this.session.member.memberId,
    status:2,
    projectIds:projectIds,
    pageNo:1,
    pageSize:20
  }
  var data = yield {
    unsign:service.page4HK(this, params1),
    signed:service.page4HK(this,params2),
    signcancel:service.page4HK(this,params3),
    projects:this.session.projects,
    curProject:this.params.project
  };

  this.body = this.render('templates/home.html',data);
});



